var t = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")), e = require("../../@babel/runtime/helpers/asyncToGenerator"), a = getApp(), s = null;

Page({
    data: {
        pageIsLoad: !1,
        is_check: 1,
        lists: [],
        message: "",
        balance: 0,
        vip_expire_time: "",
        scrollTop: 0,
        shareTitle: "",
        shareImage: "",
        timeline_share_id: 0
    },
    onLoad: function(t) {
        var e = this;
        console.log("options", t), t.sid && wx.setStorageSync("sid", t.sid), this.setData({
            system: a.globalData.system
        }), a.util.checkLogin().then(function() {
            e.getAd(), e.getWxappInfo(), e.getHistoryMsg(), e.getBalance(), e.setData({
                pageIsLoad: !0
            });
        });
    },
    onShow: function() {
        this.data.pageIsLoad && this.getBalance();
    },
    onChangeText(e) {
      this.setData({
        message: e.detail.value
      })
    },
    getHistoryMsg: function() {
        var t = this;
        a.util.request({
            url: "/wxapp/getHistoryMsg"
        }).then(function(e) {
            e.data.length > 0 && t.setData({
                lists: e.data
            }), t.scrollToBottom();
        });
    },
    getBalance: function() {
        var t = this;
        a.util.request({
            url: "/wxapp/getBalance",
            loading: !1
        }).then(function(e) {
            t.setData({
                balance: e.data.balance,
                vip_expire_time: e.data.vip_expire_time
            });
        });
    },
    getAd: function() {
        var t = this;
        a.util.request({
            url: "/wxapp/getIndexAd",
            loading: !1
        }).then(function(e) {
            t.setData({
                ad: e.data
            }), t.initInterAd();
        });
    },
    initInterAd: function() {
        var t = this.data.ad;
        if (!s && t.inter_is_open && t.inter_unit_id) {
            var e = new Date().getTime(), a = wx.getStorageSync("insertAdLastShow");
            a && e - a < 18e5 || wx.createInterstitialAd && (s = wx.createInterstitialAd({
                adUnitId: "adunit-63aa363ae570f692"
            })).onLoad(function() {
                wx.setStorageSync("insertAdLastShow", e), s.show();
            });
        }
    },
    sendText: function() {
        var t = this, e = this.data.message;
        e ? a.util.request({
            url: "/wxapp/wordFilter",
            data: {
                message: e
            },
            loading: !0
        }).then(function(s) {
            var i = s.data, n = t.data.lists;
            n.push({
                user: "我",
                message: i.split("\n")
            }), n.push({
                user: "ChatGPT",
                message: [ "正在输入.." ]
            }), t.setData({
                message: "",
                lists: n
            }), t.scrollToBottom(), a.util.request({
                url: "/wxapp/sendText",
                data: {
                    message: e
                },
                loading: !1,
                timeout: 12e4
            }).then(function(e) {
                (n = n.slice(0, -1)).push({
                    user: "ChatGPT",
                    message: e.data
                }), t.setData({
                    lists: n
                }), t.scrollToBottom(), t.getBalance();
            }).catch(function(e) {
                (n = n.slice(0, -1)).push({
                    user: "ChatGPT",
                    message: [ "网络错误，本条消息不扣费。" ]
                }), t.setData({
                    lists: n
                });
            });
        }) : console.log("no input");
    },
    sendConfirm: function() {
        var t = this;
        setTimeout(function() {
            t.sendText();
        }, 50);
    },
    scrollToBottom: function() {
        var t = this;
        setTimeout(function() {
            var e = wx.createSelectorQuery().in(t);
            e.select(".list").boundingClientRect(function(e) {
                t.setData({
                    scrollTop: e.height
                });
            }), e.exec(function(t) {});
        }, 100);
    },
    toPay: function() {
        wx.navigateTo({
            url: "/pages/pay/pay"
        });
    },
    copyText: function(t) {
        var e = t.currentTarget.dataset.index, a = this.data.lists[e].message;
        a = (a = a.join("\n")).replaceAll("&nbsp;", " "), wx.setClipboardData({
            data: a
        });
    },
    getWxappInfo: function() {
        var t = this;
        return new Promise(function(e, s) {
            a.util.request({
                url: "/wxapp/getWxappInfo",
                loading: !1
            }).then(function(a) {
                var s = a.data.page_title;
                s && wx.setNavigationBarTitle({
                    title: s
                });
                var i = a.data.welcome;
                if (i) {
                    var n = t.data.lists;
                    0 == n.length && (n.push({
                        user: "ChatGPT",
                        message: [ i ]
                    }), t.setData({
                        lists: n
                    }));
                }
                t.setData({
                    shareTitle: a.data.share_title,
                    shareImage: a.data.share_image,
                    is_check: a.data.is_check
                }), e();
            });
        });
    },
    onShareAppMessage: function() {
        var a = this;
        return e(t.default.mark(function e() {
            var s;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, a.getShareId("wechat");

                  case 2:
                    return s = t.sent, console.log("sh", s), t.abrupt("return", {
                        title: a.data.shareTitle,
                        imageUrl: a.data.shareImage,
                        path: "/pages/index/index?sid=" + s
                    });

                  case 5:
                  case "end":
                    return t.stop();
                }
            }, e);
        }))();
    },
    onShareTimeline: function() {
        var t = this.data.timeline_share_id;
        return t || this.getShareId("timeline"), console.log("share_id", t), {
            title: this.data.shareTitle,
            imageUrl: this.data.shareImage,
            query: "sid=" + t
        };
    },
    getShareId: function(s) {
        var i = this;
        return e(t.default.mark(function e() {
            var n;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return n = 0, t.next = 3, a.util.request({
                        url: "/wxapp/doShare",
                        data: {
                            way: s
                        }
                    }).then(function(t) {
                        n = t.data.share_id, "timeline" == s && i.setData({
                            timeline_share_id: n
                        });
                    });

                  case 3:
                    return t.abrupt("return", n);

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, e);
        }))();
    },
    onAddToFavorites: function() {
        return {
            title: this.data.shareTitle,
            imageUrl: this.data.shareImage
        };
    }
});